package oss.juelg.flashlight_mini;

import static oss.juelg.flashlight_mini.FlashlightActivity.TAG;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.util.Log;
import android.widget.RemoteViews;

public class FlashlightWidgetProvider extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Log.d(TAG, "Widget onUpdate()");
		
		// register event handler for widget button
		//http://developer.android.com/guide/topics/appwidgets/index.html
		
		// add Intent to notify Service
		PendingIntent pending = PendingIntent.getService(context, 0, FlashlightActivity.TOGGLE_INTENT, PendingIntent.FLAG_CANCEL_CURRENT); //use FLAG_UPDATE_CURRENT???
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		views.setOnClickPendingIntent(R.id.widget_button, pending);
		
		// there may be more than one instance of this widget
		for (int i=0; i< appWidgetIds.length; i++) {
			int appWidgetId = appWidgetIds[i];
			appWidgetManager.updateAppWidget(appWidgetId, views);	
		}
		
	}
	
	//onReceive receives the intents for onUpdate, onEnable, ... and maybe custom intents too
}
